import { Shape } from '../shapes/Shape';

export class Canvas {
    private canvas: HTMLCanvasElement;
    private ctx: CanvasRenderingContext2D;
    private width: number;
    private height: number;
    private shapes: Shape[] = [];

    constructor(containerId: string, width: number = 800, height: number = 600) {
        const container = document.getElementById(containerId);
        if (!container) {
            throw new Error('Container element not found');
        }

        this.canvas = document.createElement('canvas');
        this.canvas.width = width;
        this.canvas.height = height;
        this.width = width;
        this.height = height;
        
        container.appendChild(this.canvas);
        
        const context = this.canvas.getContext('2d');
        if (!context) {
            throw new Error('无法获取 Canvas 上下文');
        }
        this.ctx = context;
    }

    // 基础方法
    clear(): void {
        this.ctx.clearRect(0, 0, this.width, this.height);
    }

    // 获取画布尺寸
    getSize(): { width: number; height: number } {
        return {
            width: this.width,
            height: this.height
        };
    }

    // 添加新方法
    addShape(shape: Shape): void {
        this.shapes.push(shape);
        this.render();
    }

    render(): void {
        this.clear();
        this.shapes.forEach(shape => shape.draw(this.ctx));
    }

    getContext(): CanvasRenderingContext2D {
        return this.ctx;
    }
} 